var db = require('../../db/db');
var publictool = require("../../publictool/publictool");

exports.port = function (req, res) {
    var requestData = '';
    req.addListener('data', function (postDataChunk) {
        requestData += postDataChunk;
    });

    req.addListener('end', function () {
        publictool.putlog(req, requestData);
        var phone = publictool.checkoutdata(res, requestData);
        if (!phone) {
            return;
        }

        var client = db.client();
        db.checkuser(client, res, true, phone.accesstoken)
            .then(function (resultvalue) {
                var sql = 'SELECT * FROM user';
                var addSqlParams = [];
                if (phone.search_account) {
                    sql = 'SELECT * FROM user where account=?';
                    addSqlParams = [phone.search_account];
                }


                client.query(sql, addSqlParams, function (err, result) {
                    if (err) {
                        console.log('[SELECT ERROR] - ', err.message);
                        publictool.returnErr(res, '查询参数错误', 1);
                        client.end();
                        return;
                    }

                    if (result.length == 0) {
                        publictool.returnErr(res, '无此查询用户', 6);
                        client.end();
                        return;
                    }

                    var value = {
                        data: result
                    };
                    publictool.returnValue(res, value, '操作成功');
                    client.end();
                });
            });
    });
};